package com.musicserver.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.musicserver.common.R;
import com.musicserver.model.domain.Song;
import com.musicserver.model.request.SongRequest;
import com.musicserver.service.SongService;
import com.musicserver.mapper.SongMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;

/**
* @author tch001
* @description 针对表【song】的数据库操作Service实现
* @createDate 2025-09-16 08:37:57
*/
@Service
public class SongServiceImpl extends ServiceImpl<SongMapper, Song>
    implements SongService{

    @Autowired
    private SongMapper songMapper;

    @Override
    public R songOfSingerId(int singerId) {
        QueryWrapper<Song> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("singer_id",singerId);
        return R.success(null, songMapper.selectList(queryWrapper));
    }

    @Override
    public R addSong(SongRequest addSongRequest, MultipartFile mpfile) {
        Song song = new Song();
        BeanUtils.copyProperties(addSongRequest, song);
        String pic = "/img/songPic/tubiao.jpg";
        String fileName = System.currentTimeMillis() + "-" + mpfile.getOriginalFilename();
        String filePath = System.getProperty("user.dir") + System.getProperty("file.separator") + "song" ;
        File file = new File(filePath);
        if(!file.exists())
            if(!file.mkdir()){
                return R.fatal("创建文件夹失败");
            }
        File dest = new File(filePath + System.getProperty("file.separator") + fileName);
        String storeUrlPath = "/song/" + fileName;
        try{
            mpfile.transferTo(dest);
        }catch(IOException e){
            return R.fatal("上传失败" + e.getMessage());
        }
        song.setCreateTime(new Date());
        song.setUpdateTime(new Date());
        song.setPic(pic);
        song.setUrl(storeUrlPath);
        if (songMapper.insert(song) > 0) {
            return R.success("上传成功", storeUrlPath);
        } else {
            return R.error("上传失败");
        }
    }

    @Override
    public R updateSongMsg(SongRequest updateSongRequest) {
        Song song = new Song();
        BeanUtils.copyProperties(updateSongRequest, song);
        song.setUpdateTime(new Date());
        if (songMapper.updateById(song) > 0) {
            return R.success("修改成功");
        } else {
            return R.error("修改失败");
        }
    }

    @Override
    public R deleteSong(Integer id) {
        if (songMapper.deleteById(id) > 0) {
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

    @Override
    public R deleteSongs(String[] ids){
        StringBuffer sb = new StringBuffer();
        for(String id: ids){
            sb.append(id+",");
        }
        sb.deleteCharAt(sb.length()-1);
        if(songMapper.deleteByIds(sb.toString()) > 0){
            return R.success("删除成功");
        }else{
            return R.error("删除失败");
        }
    }

    @Override
    public R songOfSongListId(int songListId) {
        return R.success("查询成功", songMapper.selectSongListBySongListId(songListId));
    }

    @Override
    public R allSong() {
        return R.success("查询成功", songMapper.selectList(null));
    }

    @Override
    public R songOfSongName(String name) {
        QueryWrapper<Song> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", name); // where name like '%name%'
        return R.success("查询成功", songMapper.selectList(queryWrapper));
    }
}




